﻿<Window
    x:Class="Gumini.JulaDB.Navigator.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ae="clr-namespace:ICSharpCode.AvalonEdit;assembly=ICSharpCode.AvalonEdit"
    Title="JulaDB Navigator"
    Height="700" Width="1000">

    <Window.Resources>

        <ResourceDictionary>
            <Style
                x:Key="ToolbarButton" TargetType="Button" BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
                <Setter Property="Height" Value="40" />
                <Setter Property="Width" Value="100" />
                <Setter Property="BorderThickness" Value="1" />
                <Setter Property="BorderBrush" Value="Gray" />
            </Style>
        </ResourceDictionary>

    </Window.Resources>

    <DockPanel
        x:Name="LayoutRoot"
        LastChildFill="True">

        <StackPanel
            DockPanel.Dock="Top"
            Orientation="Horizontal">

            <Button
                Style="{StaticResource ToolbarButton}"
                Content="Connect..."
                Command="{Binding OpenConnectionWindowCommand}" />

            <Button
                Style="{StaticResource ToolbarButton}"
                Content="Disconnect"
                Command="{Binding DisconnectCommand}" />

            <Button
                Style="{StaticResource ToolbarButton}"
                Content="Execute"
                Command="{Binding ExecuteCommand}" />

            <Button
                Style="{StaticResource ToolbarButton}"
                Content="Save SQL file"
                Command="{Binding SaveScriptCommand}" />

            <CheckBox
                Margin="20,0"
                Content="Use query optimizer"
                VerticalAlignment="Center"
                IsChecked="{Binding UseOptimizer}" />
            
        </StackPanel>

        <StatusBar
            DockPanel.Dock="Bottom">

            <TextBlock
                Margin="10,0"
                Text="Status:" />

            <TextBlock
                Margin="0,0,10,0">
                
                <TextBlock.Style>
                    <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=Connection.IsConnected}" Value="True">
                                <Setter Property="TextBlock.Text" Value="connected"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Path=Connection.IsConnected}" Value="False">
                                  <Setter Property="TextBlock.Text" Value="not connected"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>        
            </TextBlock>

            <Separator
                Margin="0,0,10,0"/>

            <TextBlock
                Margin="0,0,10,0"
                Text="Execution time [s]:" />

            <TextBlock
                Margin="0,0,10,0"
                Text="{Binding Path=ExecutionTime}" />

            <Separator
                Margin="0,0,10,0"/>
            
            <TextBlock
                Margin="0,0,10,0"
                Text="Data source:" />

            <TextBlock
                Margin="0,0,10,0"
                Text="{Binding Path=Connection.DataSource}" />

            <Separator
                Margin="0,0,10,0"/>

            <TextBlock
                Margin="0,0,10,0"
                Text="SQL file:" />

            <TextBlock
                Margin="0,0,10,0"
                Text="{Binding Path=Script.Path}" />

        </StatusBar>
        
        <Border
            BorderBrush="LightSteelBlue" 
            BorderThickness="3" >
        
            <Grid>
            
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <GridSplitter
                    Grid.Column="1"
                    HorizontalAlignment="Center" VerticalAlignment="Stretch"
                    Background="LightSteelBlue" 
                    ShowsPreview="True"
                    Width="4" />

                <Grid
                    Grid.Column="0">

                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>

                    <GridSplitter
                        Grid.Row="1"
                        HorizontalAlignment="Stretch" VerticalAlignment="Center"
                        Background="LightSteelBlue" 
                        ShowsPreview="True"
                        Height="4" />

                    <GroupBox
                        Grid.Row="0"
                        Header="Database query">
                    
                        <ae:TextEditor
                            Margin="3"
                            FontFamily="Consolas" FontSize="14"
                            VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"
                            ShowLineNumbers="True"
                            Document="{Binding Path=Document}" />

                    </GroupBox>

                    <GroupBox
                        Grid.Row="2"
                        Header="Query results">

                        <DataGrid
                            Margin="3"
                            AutoGenerateColumns="False" 
                            ItemsSource="{Binding Path=Rows}">
                            
                            <DataGrid.Columns>
                                <DataGridTemplateColumn Width="*">
                                    
                                    <DataGridTemplateColumn.HeaderStyle>
                                        <Style TargetType="DataGridColumnHeader">
                                            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                                            <Setter Property="VerticalContentAlignment" Value="Stretch" />
                                            <Setter Property="Margin" Value="0" />
                                            <Setter Property="ContentTemplate">
                                                <Setter.Value>
                                                    <DataTemplate>
                                                        <ItemsControl
                                                            ItemsSource="{Binding DataContext.ColumnHeaders, ElementName=LayoutRoot}">
                                                            <ItemsControl.ItemsPanel>
                                                                <ItemsPanelTemplate>
                                                                    <StackPanel Orientation="Horizontal" />
                                                                </ItemsPanelTemplate>
                                                            </ItemsControl.ItemsPanel>
                                                            <ItemsControl.ItemTemplate>
                                                                <DataTemplate>
                                                                    <Border Width="70">
                                                                        <TextBlock Text="{Binding}" TextAlignment="Center"/>
                                                                    </Border>
                                                                </DataTemplate>
                                                            </ItemsControl.ItemTemplate>
                                                        </ItemsControl>
                                                    </DataTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </DataGridTemplateColumn.HeaderStyle>
                                    
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <ItemsControl ItemsSource="{Binding Path=Items}">
                                                <ItemsControl.ItemsPanel>
                                                    <ItemsPanelTemplate>
                                                        <StackPanel Orientation="Horizontal"/>
                                                    </ItemsPanelTemplate>
                                                </ItemsControl.ItemsPanel>
                                                <ItemsControl.ItemTemplate>
                                                    <DataTemplate>
                                                        <Border Width="70">
                                                            <TextBlock Text="{Binding}" TextAlignment="Center"/>
                                                        </Border>
                                                    </DataTemplate>
                                                </ItemsControl.ItemTemplate>
                                            </ItemsControl>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                    
                                </DataGridTemplateColumn>
                            </DataGrid.Columns>
                        </DataGrid>

                    </GroupBox>

                </Grid>

                <GroupBox
                    Grid.Column="2"
                    Header="Message log">

                    <DataGrid
                        Margin="3"
                        ItemsSource="{Binding Path=LogMessages}"
                        AutoGenerateColumns="False">
                        
                        <DataGrid.Columns>

                            <DataGridTemplateColumn Header="Time" Width="60">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock TextWrapping="Wrap" Text="{Binding Path=Time, StringFormat=hh.mm.ss.fffffff}" />
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>

                            <DataGridTextColumn
                                Header="Type" Width="*" IsReadOnly="True"
                                Binding="{Binding Path=Type}" />

                            <DataGridTextColumn
                                Header="Line" Width="30" IsReadOnly="True"
                                Binding="{Binding Path=Position.LineNumber}" />

                            <DataGridTextColumn
                                Header="Column" Width="30" IsReadOnly="True"
                                Binding="{Binding Path=Position.ColumnNumber}" />

                            <DataGridTemplateColumn Header="Message" Width="4*">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock TextWrapping="Wrap" Text="{Binding Path=Message}" />
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            
                        </DataGrid.Columns>
                        
                    </DataGrid>

                </GroupBox>

            </Grid>

        </Border>

    </DockPanel>

</Window>
